package tree;

/**
 * 结点删除与查找
 * @Auther Jun jie Yi
 * @Date 22:08 2021/7/16
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        //构造一个结构
        HeroNode root = new BinaryTreeDemo().new HeroNode(1, "宋江");
        root.left = new BinaryTreeDemo().new HeroNode(2, "吴用");
        root.right = new BinaryTreeDemo().new HeroNode(3, "卢梭");
        root.right.right = new BinaryTreeDemo().new HeroNode(4, "林冲");

        BinaryTreeDemo.delNode(1, root);
        BinaryTreeDemo.look(root);
    }

    public static void delNode(int no, HeroNode T) {
        if(T.left != null && T.left.no == no) {
            T.left = null;
            return;
        }
        if(T.right != null && T.right.no == no) {
            T.right = null;
            return;
        }
        if(T.left != null) {
            delNode(no, T.left);
        }
        if(T.right != null) {
            delNode(no, T.right);
        }
    }

    public static void look(HeroNode T) {
        System.out.println(T.name);
        if(T.left != null) {
            look(T.left);
        }
        if(T.right != null) {
            look(T.right);
        }
    }

    class HeroNode {
        int no;
        String name;
        HeroNode left;
        HeroNode right;
        public HeroNode(int no, String name) {
            this.no = no;
            this.name = name;
        }
    }
}

